<html>
  <head>
  <title>bustersGhostAgents.py</title>
  </head>
  <body>
  <h3>bustersGhostAgents.py (<a href="../bustersGhostAgents.py">original</a>)</h3>
  <hr>
  <pre>
<span style="color: green; font-style: italic"># bustersGhostAgents.py
# ---------------------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html

</span><span style="color: blue; font-weight: bold">import </span>ghostAgents
<span style="color: blue; font-weight: bold">from </span>game <span style="color: blue; font-weight: bold">import </span>Directions
<span style="color: blue; font-weight: bold">from </span>game <span style="color: blue; font-weight: bold">import </span>Actions
<span style="color: blue; font-weight: bold">from </span>util <span style="color: blue; font-weight: bold">import </span>manhattanDistance
<span style="color: blue; font-weight: bold">import </span>util

<span style="color: blue; font-weight: bold">class </span>StationaryGhost<span style="font-weight: bold">( </span>ghostAgents<span style="font-weight: bold">.</span>GhostAgent <span style="font-weight: bold">):
  </span><span style="color: blue; font-weight: bold">def </span>getDistribution<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state <span style="font-weight: bold">):
    </span>dist <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">()
    </span>dist<span style="font-weight: bold">[</span>Directions<span style="font-weight: bold">.</span>STOP<span style="font-weight: bold">] = </span><span style="color: red">1.0
    </span><span style="color: blue; font-weight: bold">return </span>dist
  
<span style="color: blue; font-weight: bold">class </span>DispersingGhost<span style="font-weight: bold">( </span>ghostAgents<span style="font-weight: bold">.</span>GhostAgent <span style="font-weight: bold">):
  </span><span style="color: red">"Chooses an action that distances the ghost from the other ghosts with probability spreadProb."
  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">, </span>index<span style="font-weight: bold">, </span>spreadProb<span style="font-weight: bold">=</span><span style="color: red">0.5</span><span style="font-weight: bold">):
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index <span style="font-weight: bold">= </span>index
    <span style="color: blue">self</span><span style="font-weight: bold">.</span>spreadProb <span style="font-weight: bold">= </span>spreadProb
      
  <span style="color: blue; font-weight: bold">def </span>getDistribution<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state <span style="font-weight: bold">):
    </span>ghostState <span style="font-weight: bold">= </span>state<span style="font-weight: bold">.</span>getGhostState<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index <span style="font-weight: bold">)
    </span>legalActions <span style="font-weight: bold">= </span>state<span style="font-weight: bold">.</span>getLegalActions<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index <span style="font-weight: bold">)
    </span>pos <span style="font-weight: bold">= </span>state<span style="font-weight: bold">.</span>getGhostPosition<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index <span style="font-weight: bold">)
    </span>isScared <span style="font-weight: bold">= </span>ghostState<span style="font-weight: bold">.</span>scaredTimer <span style="font-weight: bold">&gt; </span><span style="color: red">0
    
    </span>speed <span style="font-weight: bold">= </span><span style="color: red">1
    </span><span style="color: blue; font-weight: bold">if </span>isScared<span style="font-weight: bold">: </span>speed <span style="font-weight: bold">= </span><span style="color: red">0.5
    </span>actionVectors <span style="font-weight: bold">= [</span>Actions<span style="font-weight: bold">.</span>directionToVector<span style="font-weight: bold">( </span>a<span style="font-weight: bold">, </span>speed <span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>a <span style="color: blue; font-weight: bold">in </span>legalActions<span style="font-weight: bold">]
    </span>newPositions <span style="font-weight: bold">= [( </span>pos<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]+</span>a<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">], </span>pos<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]+</span>a<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] ) </span><span style="color: blue; font-weight: bold">for </span>a <span style="color: blue; font-weight: bold">in </span>actionVectors<span style="font-weight: bold">]

    </span><span style="color: green; font-style: italic"># get other ghost positions
    </span>others <span style="font-weight: bold">= [</span>i <span style="color: blue; font-weight: bold">for </span>i <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">,</span>state<span style="font-weight: bold">.</span>getNumAgents<span style="font-weight: bold">()) </span><span style="color: blue; font-weight: bold">if </span>i <span style="font-weight: bold">!= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index<span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">for </span>a <span style="color: blue; font-weight: bold">in </span>others<span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">assert </span>state<span style="font-weight: bold">.</span>getGhostState<span style="font-weight: bold">(</span>a<span style="font-weight: bold">) != </span><span style="color: blue">None</span><span style="font-weight: bold">, </span><span style="color: red">"Ghost position unspecified in state!"
    </span>otherGhostPositions <span style="font-weight: bold">= [</span>state<span style="font-weight: bold">.</span>getGhostPosition<span style="font-weight: bold">(</span>a<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>a <span style="color: blue; font-weight: bold">in </span>others <span style="color: blue; font-weight: bold">if </span>state<span style="font-weight: bold">.</span>getGhostPosition<span style="font-weight: bold">(</span>a<span style="font-weight: bold">)[</span><span style="color: red">1</span><span style="font-weight: bold">] &gt; </span><span style="color: red">1</span><span style="font-weight: bold">]
    
    </span><span style="color: green; font-style: italic"># for each action, get the sum of inverse squared distances to the other ghosts
    </span>sumOfDistances <span style="font-weight: bold">= []
    </span><span style="color: blue; font-weight: bold">for </span>pos <span style="color: blue; font-weight: bold">in </span>newPositions<span style="font-weight: bold">:
      </span>sumOfDistances<span style="font-weight: bold">.</span>append<span style="font-weight: bold">( </span>sum<span style="font-weight: bold">([(</span><span style="color: red">1</span><span style="font-weight: bold">+</span>manhattanDistance<span style="font-weight: bold">(</span>pos<span style="font-weight: bold">, </span>g<span style="font-weight: bold">))**(-</span><span style="color: red">2</span><span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>g <span style="color: blue; font-weight: bold">in </span>otherGhostPositions<span style="font-weight: bold">]) )

    </span>bestDistance <span style="font-weight: bold">= </span>min<span style="font-weight: bold">(</span>sumOfDistances<span style="font-weight: bold">)
    </span>numBest <span style="font-weight: bold">= [</span>bestDistance <span style="font-weight: bold">== </span>dist <span style="color: blue; font-weight: bold">for </span>dist <span style="color: blue; font-weight: bold">in </span>sumOfDistances<span style="font-weight: bold">].</span>count<span style="font-weight: bold">(</span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">)
    </span>distribution <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">for </span>action<span style="font-weight: bold">, </span>distance <span style="color: blue; font-weight: bold">in </span>zip<span style="font-weight: bold">(</span>legalActions<span style="font-weight: bold">, </span>sumOfDistances<span style="font-weight: bold">):
      </span><span style="color: blue; font-weight: bold">if </span>distance <span style="font-weight: bold">== </span>bestDistance<span style="font-weight: bold">: </span>distribution<span style="font-weight: bold">[</span>action<span style="font-weight: bold">] += </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>spreadProb <span style="font-weight: bold">/ </span>numBest
      distribution<span style="font-weight: bold">[</span>action<span style="font-weight: bold">] += (</span><span style="color: red">1 </span><span style="font-weight: bold">- </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>spreadProb<span style="font-weight: bold">) / </span>len<span style="font-weight: bold">(</span>legalActions<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">return </span>distribution

  </pre>
  </body>
  </html>
  